package com.sunday.volunteer.admin.ui.index;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.admin.BaseApp;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.GroupType;
import com.sunday.volunteer.admin.entity.Member;
import com.sunday.volunteer.admin.event.UpdateMemberEvent;
import com.sunday.volunteer.admin.http.VolunteerClient;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 修改用户信息
* */
public class UpdateMemberInfoActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.spinner_type)
    Spinner spinnerType;
    @Bind(R.id.l_type)
    LinearLayout lType;
    @Bind(R.id.et_info)
    ClearEditText etInfo;
    @Bind(R.id.l_info)
    LinearLayout lInfo;
    @Bind(R.id.rightTxt)
    TextView rightTxt;

    private Member member;

    private int update_type;//1,组织类型，2，名称，3，服务范围，4，规模
    private ArrayAdapter<String> adapter;
    private List<GroupType> groupTypeList;
    private List<String> list = new ArrayList<String>();
    private String groType,info,name,logo,mobileName,groFor;
    private int nums;//规模

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_member_info);
        ButterKnife.bind(this);
        rightTxt.setVisibility(View.VISIBLE);
        rightTxt.setText("保存");
        member = BaseApp.getInstance().getMember();
        update_type = getIntent().getIntExtra("update_type",-1);
        if (update_type==1){
            lInfo.setVisibility(View.GONE);
            titleView.setText("组织类型");
            spinnerAdapter();
            getType();
        }else if (update_type==2){
            lType.setVisibility(View.GONE);
            titleView.setText("组织全称");
            etInfo.setText(member.getName());
        }else if (update_type==3){
            lType.setVisibility(View.GONE);
            titleView.setText("组织服务范围");
            etInfo.setText(member.getServiceArea());
        }else if (update_type==4){
            lType.setVisibility(View.GONE);
            titleView.setText("组织规模");
            etInfo.setText(member.getLimitNumber()+"");
        }
    }

    @OnClick(R.id.rightTxt)
    public void onClick() {
        if (update_type==1){
            updateInfo(null,null,null,null,null,groType);
        }else if (update_type==2){
            info = etInfo.getText().toString().trim();
            if (TextUtils.isEmpty(info)){
                ToastUtils.showToast(mContext,"输入组织全称");
                return;
            }
            updateInfo(info,null,null,null,null,null);
        }else if (update_type==3){
            info = etInfo.getText().toString().trim();
            if (TextUtils.isEmpty(info)){
                ToastUtils.showToast(mContext,"输入组织服务范围");
                return;
            }
            updateInfo(null,null,null,null,info,null);
        }else if (update_type==4){
            info = etInfo.getText().toString().trim();
            try {
                nums = Integer.parseInt(info);
                if (nums<=0){
                    ToastUtils.showToast(mContext,"只能输入正整数");
                    return;
                }
            }catch (Exception e){
                ToastUtils.showToast(mContext,"只能输入正整数");
                return;
            }
            updateInfo(null,null,null,nums,null,null);
        }
    }

    //更新
    private void updateInfo(String name,String logo,String mobileName,Integer nums,String groFor,String groType){
        showLoadingDialog(0);
        Call<ResultDO<Member>> call = VolunteerClient.getVolunteerAdapter().updateMember(member.getId(),name,logo,mobileName,nums,groFor,groType);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    member = response.body().getResult();
                    SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                    EventBus.getDefault().post(new UpdateMemberEvent());
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    private void spinnerAdapter(){
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerType.setAdapter(adapter);
        spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                groType = groupTypeList.get(position).getName();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    //组织类型
    private void getType(){
        showLoadingDialog(0);
        Call<ResultDO<List<GroupType>>> call = VolunteerClient.getVolunteerAdapter().select("groupType");
        call.enqueue(new Callback<ResultDO<List<GroupType>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<GroupType>>> call, Response<ResultDO<List<GroupType>>> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    groupTypeList = response.body().getResult();
                    list.clear();
                    for (int i = 0;i<groupTypeList.size();i++){
                        list.add(groupTypeList.get(i).getName());
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<GroupType>>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }
}
